{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff604bdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#问题二附件3语音业务打分预测代码\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from lightgbm import LGBMClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.multiclass import OneVsRestClassifier\n",
    "from sklearn.ensemble import VotingClassifier\n",
    "from sklearn.multiclass import OneVsOneClassifier\n",
    "from mlxtend.classifier import StackingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b35767cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_3 = pd.read_excel('./data/data_1处理后的数据.xlsx').drop('Unnamed: 0', axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16042325",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多标签多分类问题\n",
    "df1 = data_3.copy()\n",
    "df1 = pd.get_dummies(df1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56290dc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基模型选择随机森林时\n",
    "from sklearn.multioutput import ClassifierChain\n",
    "from sklearn.metrics import jaccard_score\n",
    "X_train, X_test, y_train, y_test = train_test_split(df1.iloc[:, 4 : ], df1.iloc[:, :4], test_size = 0.3, random_state = 10)\n",
    "model_1 = RandomForestClassifier()\n",
    "chains = [ClassifierChain(model_1, order='random', random_state=i)\n",
    "          for i in range(10)]\n",
    "for chain in chains:\n",
    "    chain.fit(X_train, y_train)\n",
    "Y_pred_chains = np.array([chain.predict(X_test) for chain in chains], dtype = 'int64')\n",
    "\n",
    "jac_scores = []\n",
    "for i in range(len(chains)):\n",
    "    for j in range(len(y_test.to_numpy())):\n",
    "        a = list(y_test.to_numpy()[j])\n",
    "        b = list(Y_pred_chains[i][j])\n",
    "        jac_score = jaccard_score(a, b, average = None)\n",
    "        jac_scores.append(jac_score[0])\n",
    "    sums = 0\n",
    "    for n in jac_scores:\n",
    "        sums += n\n",
    "    avg = sums / len(jac_scores)\n",
    "    print('第%i条链的平均Jaccard相似系数为:%.3f'%((i + 1), avg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7774c4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基模型选择LightGBM时\n",
    "model_3 = LGBMClassifier()\n",
    "chains = [ClassifierChain(model_3, order='random', random_state=i)\n",
    "          for i in range(10)]\n",
    "for chain in chains:\n",
    "    chain.fit(X_train, y_train)\n",
    "Y_pred_chains = np.array([chain.predict(X_test) for chain in chains], dtype = 'int64')\n",
    "\n",
    "\n",
    "jac_scores = []\n",
    "for i in range(len(chains)):\n",
    "    for j in range(len(y_test.to_numpy())):\n",
    "        a = list(y_test.to_numpy()[j])\n",
    "        b = list(Y_pred_chains[i][j])\n",
    "        jac_score = jaccard_score(a, b, average = None)\n",
    "        jac_scores.append(jac_score[0])\n",
    "    sums = 0\n",
    "    for n in jac_scores:\n",
    "        sums += n\n",
    "    avg = sums / len(jac_scores)\n",
    "    print('第%i条链的平均Jaccard相似系数为:%.3f'%((i + 1), avg))\n",
    "\n",
    "    \n",
    "for i in range(len(chains)):\n",
    "    y_pred = pd.DataFrame(Y_pred_chains[i])\n",
    "    scores = []\n",
    "    for j in range(4):\n",
    "        score = accuracy_score(y_test.iloc[:, j], y_pred.iloc[:, j])\n",
    "        scores.append(score)\n",
    "    print('第%i条链的准确率为:'%((i + 1)))\n",
    "    print(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ce164bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 语音整理满意度\n",
    "\"\"\"\n",
    "不采用过采样\n",
    "\"\"\"\n",
    "## 采用100%的特征\n",
    "cols = ['语音通话整体满意度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断', '套外流量费（元）', '手机没有信号', '省际漫游-时长（分钟）', '套外流量（MB）', \\\n",
    "            '当月欠费金额', '居民小区', '通话过程中一方听不见', '地铁', '高校', '高铁', '其他，请注明.1', '外省语音占比', '外省流量占比', \\\n",
    "                '商业街', '农村', '串线', '其他，请注明']\n",
    "df_3 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_3[i] = data_3[i]\n",
    "\n",
    "\n",
    "df_3 = pd.get_dummies(df_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b573144",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 尝试采用投票法\n",
    "from sklearn.ensemble import VotingClassifier\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(n_estimators =300, random_state=10)\n",
    "clf2 = RandomForestClassifier(n_estimators = 300, max_depth = 5, random_state = 10)\n",
    "clf3 = LGBMClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(n_estimators = 300, random_state = 10, max_depth = 5)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f898d55",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 尝试模型融合\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4534bf7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 尝试对随机森林进行调参\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf2.fit(X_train, y_train)\n",
    "y_pred = clf2.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))\n",
    "print(cross_val_score(clf2, X_train, y_train, cv = 5, scoring = 'accuracy'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e38f55c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "param = {\"criterion\": [\"gini\", \"entropy\"], \"max_depth\": np.arange(1, 20, 1), 'n_estimators': np.arange(100, 501, 50)} \n",
    "rf_gs = GridSearchCV(estimator = RandomForestClassifier(), param_grid = param, cv = 5, scoring = \"accuracy\", n_jobs = -1, verbose = 10) \n",
    "rf_gs.fit(X_train, y_train) \n",
    "print(rf_gs.best_params_) \n",
    "y_hat = rf_gs.best_estimator_.predict(X_test) \n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e93b1728",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVR算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95927be7",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVR算法的随机森林调参\n",
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d712d89",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVO算法\n",
    "from sklearn.multiclass import OneVsOneClassifier\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_3.iloc[:, 1 : ], df_3.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51416f9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVO算法对随机森林进行调参\n",
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsOneClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9aa43eb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 特征选取前80%\n",
    "cols = ['语音通话整体满意度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断', '套外流量费（元）', '手机没有信号', '省际漫游-时长（分钟）', '套外流量（MB）', \\\n",
    "            '当月欠费金额', '居民小区', '通话过程中一方听不见', '地铁', '高校', '高铁']\n",
    "df_4 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_4[i] = data_3[i]\n",
    "\n",
    "\n",
    "df_4 = pd.get_dummies(df_4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c2b28e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 投票法\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(n_estimators =300, random_state=10)\n",
    "clf2 = RandomForestClassifier(n_estimators = 300, max_depth = 5, random_state = 10)\n",
    "clf3 = LGBMClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(n_estimators = 300, random_state = 10, max_depth = 5)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "016c7573",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 模型融合\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "286abf3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 对随机森林进行调参\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "param = {\"criterion\": [\"gini\", \"entropy\"], \"max_depth\": np.arange(1, 20, 1), 'n_estimators': np.arange(100, 501, 50)}\n",
    "rf_gs = GridSearchCV(estimator = RandomForestClassifier(), param_grid = param, cv = 5, scoring = \"accuracy\", n_jobs = -1, verbose = 10)\n",
    "rf_gs.fit(X_train, y_train)\n",
    "print(rf_gs.best_params_)\n",
    "y_hat = rf_gs.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a1379aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVR算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ede69de",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49810d60",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed8acdfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVO算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9712f606",
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsOneClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ef124e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选取前50%的特征\n",
    "cols = ['语音通话整体满意度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断']\n",
    "df_5 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_5[i] = data_3[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eb85299",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_5 = pd.get_dummies(df_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4cbf44a",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 投票法\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_5.iloc[:, 1 : ], df_5.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(n_estimators =300, random_state=10)\n",
    "clf2 = RandomForestClassifier(n_estimators = 300, max_depth = 5, random_state = 10)\n",
    "clf3 = LGBMClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(n_estimators = 300, random_state = 10, max_depth = 5)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cae428c",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 模型融合\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9ee91b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 调参\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_5.iloc[:, 1 : ], df_5.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "param = {\"criterion\": [\"gini\", \"entropy\"], \"max_depth\": np.arange(1, 20, 1), 'n_estimators': np.arange(100, 501, 50)}\n",
    "rf_gs = GridSearchCV(estimator = RandomForestClassifier(), param_grid = param, cv = 5, scoring = \"accuracy\", n_jobs = -1, verbose = 10)\n",
    "rf_gs.fit(X_train, y_train)\n",
    "print(rf_gs.best_params_)\n",
    "y_hat = rf_gs.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d90cce8",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVR算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_5.iloc[:, 1 : ], df_5.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e54ac255",
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f52f2f74",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVO算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_4.iloc[:, 1 : ], df_4.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca99c5fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsOneClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4f01093",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取前20%的特征\n",
    "cols = ['语音通话整体满意度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数']\n",
    "df_6 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_6[i] = data_3[i]\n",
    "\n",
    "\n",
    "df_6 = pd.get_dummies(df_6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41e85673",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 投票法\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(n_estimators =300, random_state=10)\n",
    "clf2 = RandomForestClassifier(n_estimators = 300, max_depth = 5, random_state = 10)\n",
    "clf3 = LGBMClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(n_estimators = 300, random_state = 10, max_depth = 5)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a640a4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 模型融合\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a612a4ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 调参\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "param = {\"criterion\": [\"gini\", \"entropy\"], \"max_depth\": np.arange(1, 20, 1), 'n_estimators': np.arange(100, 501, 50)}\n",
    "rf_gs = GridSearchCV(estimator = RandomForestClassifier(), param_grid = param, cv = 5, scoring = \"accuracy\", n_jobs = -1, verbose = 10)\n",
    "rf_gs.fit(X_train, y_train)\n",
    "print(rf_gs.best_params_)\n",
    "y_hat = rf_gs.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f001e499",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVR算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce04708f",
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74a19d11",
   "metadata": {},
   "outputs": [],
   "source": [
    "## OVO算法\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_6.iloc[:, 1 : ], df_6.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(n_estimators = 300 , max_depth = 5, random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10, max_depth = 5)\n",
    "m3 = GradientBoostingClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m4 = LGBMClassifier(random_state = 10, n_estimators = 300, max_depth = 5)\n",
    "m5 = XGBClassifier(random_state = 10, n_estimators = 300)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['r'] + ['b'] * (len(scores))\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36e97e6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "param = {\"estimator__max_depth\": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}\n",
    "model = OneVsOneClassifier(RandomForestClassifier(random_state = 10))\n",
    "clf = GridSearchCV(model, param_grid = param, scoring = 'accuracy', n_jobs = -1, cv = 5, verbose = 2)\n",
    "clf.fit(X_train, y_train)\n",
    "print(clf.best_params_)\n",
    "y_hat = clf.best_estimator_.predict(X_test)\n",
    "print(classification_report(y_test, y_hat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2b7c174",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 开始采用过采样\n",
    "cols = ['语音通话整体满意度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断']\n",
    "df_7 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_7[i] = data_3[i]\n",
    "\n",
    "\n",
    "df_7 = pd.get_dummies(df_7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cab5978",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import SMOTE\n",
    "x = df_7.iloc[:, 1 :]\n",
    "y = df_7.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de3cb52f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84b20fe4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d53ead6",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e433f41",
   "metadata": {},
   "outputs": [],
   "source": [
    "score = cross_val_score(model, X_train, y_train - 1, cv = 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dab3e630",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51999501",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfa1a3d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98d7c274",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsOneClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33398f57",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel('./data/附件3语音业务用户满意度预测数据.xlsx')\n",
    "data = data.drop('用户id', axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64d4e922",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_8 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_8[i] = data[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c38a046f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_8['是否遇到过网络问题'].replace(1, '是', inplace = True)\n",
    "df_8['是否遇到过网络问题'].replace(2, '否', inplace = True)\n",
    "df_8['通话中有杂音、听不清、断断续续'].replace(-1, '没有', inplace = True)\n",
    "df_8['通话中有杂音、听不清、断断续续'].replace(4, '有', inplace = True)\n",
    "df_8['有信号无法拨通'].replace(-1, '没有', inplace = True)\n",
    "df_8['有信号无法拨通'].replace(2, '有', inplace = True)\n",
    "df_8['办公室'].replace(-1, '不在', inplace = True)\n",
    "df_8['办公室'].replace(2, '在', inplace = True)\n",
    "df_8['通话过程中突然中断'].replace(-1, '没有', inplace = True)\n",
    "df_8['通话过程中突然中断'].replace(3, '有', inplace = True)\n",
    "df_8 = pd.get_dummies(df_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f0ac046",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['语音通话整体满意度' ,'是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_9 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_9[i] = data_3[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d14b3726",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import SMOTE\n",
    "df_9 = pd.get_dummies(df_9)\n",
    "x = df_9.iloc[:, 1 :]\n",
    "y = df_9.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92bd0142",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(n_estimators = 300, max_depth = 5, random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d72b41b",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_1 = model.predict(df_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1db9c7aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_data = pd.read_excel('./result.xlsx')\n",
    "pred_data['语音通话整体满意度'] = y_pred_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b93a4d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['网络覆盖与信号强度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断']\n",
    "df_10 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_10[i] = data_3[i]\n",
    "df_10 = pd.get_dummies(df_10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b15ef0c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import SMOTE\n",
    "x = df_10.iloc[:, 1 :]\n",
    "y = df_10.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis = 1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7b2f506",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8a8c549",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3455cb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93aed52e",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a682ce0",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22edc999",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c452dd99",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsOneClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train, cv = 5))\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5e78b1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_11 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_11[i] = data[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e490bbd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_11['是否遇到过网络问题'].replace(1, '是', inplace = True)\n",
    "df_11['是否遇到过网络问题'].replace(2, '否', inplace = True)\n",
    "df_11['通话中有杂音、听不清、断断续续'].replace(-1, '没有', inplace = True)\n",
    "df_11['通话中有杂音、听不清、断断续续'].replace(4, '有', inplace = True)\n",
    "df_11['有信号无法拨通'].replace(-1, '没有', inplace = True)\n",
    "df_11['有信号无法拨通'].replace(2, '有', inplace = True)\n",
    "df_11['办公室'].replace(-1, '不在', inplace = True)\n",
    "df_11['办公室'].replace(2, '在', inplace = True)\n",
    "df_11['通话过程中突然中断'].replace(-1, '没有', inplace = True)\n",
    "df_11['通话过程中突然中断'].replace(3, '有', inplace = True)\n",
    "df_11 = pd.get_dummies(df_11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ece5dc04",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['网络覆盖与信号强度' ,'是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_12 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_12[i] = data_3[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "420684be",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_12 = pd.get_dummies(df_12)\n",
    "x = df_12.iloc[:, 1 :]\n",
    "y = df_12.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67b62c24",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9395f679",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_2 = model.predict(df_11)\n",
    "pred_data['网络覆盖与信号强度'] = y_pred_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "333e4f01",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['语音通话清晰度', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断']\n",
    "df_13 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_13[i] = data_3[i]\n",
    "df_13 = pd.get_dummies(df_13)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da20ba54",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = df_13.iloc[:, 1 :]\n",
    "y = df_13.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dda7583",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "clf4 = GradientBoostingClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3), ('gbdt', clf4)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d00193c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'GBDT', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76715b72",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "680e2e45",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))\n",
    "\n",
    "\n",
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "985230ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9dbf9a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "score = cross_val_score(model, X_train, y_train - 1, cv = 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbfaaefd",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6894434",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))\n",
    "\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "772aa311",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsOneClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90ec50b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_14 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_14[i] = data[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "515aed27",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_14['是否遇到过网络问题'].replace(1, '是', inplace = True)\n",
    "df_14['是否遇到过网络问题'].replace(2, '否', inplace = True)\n",
    "df_14['通话中有杂音、听不清、断断续续'].replace(-1, '没有', inplace = True)\n",
    "df_14['通话中有杂音、听不清、断断续续'].replace(4, '有', inplace = True)\n",
    "df_14['有信号无法拨通'].replace(-1, '没有', inplace = True)\n",
    "df_14['有信号无法拨通'].replace(2, '有', inplace = True)\n",
    "df_14['办公室'].replace(-1, '不在', inplace = True)\n",
    "df_14['办公室'].replace(2, '在', inplace = True)\n",
    "df_14['通话过程中突然中断'].replace(-1, '没有', inplace = True)\n",
    "df_14['通话过程中突然中断'].replace(3, '有', inplace = True)\n",
    "df_14 = pd.get_dummies(df_14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09bd1156",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['语音通话清晰度' ,'是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_15 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_15[i] = data_3[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b267ad87",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_15 = pd.get_dummies(df_15)\n",
    "x = df_15.iloc[:, 1 :]\n",
    "y = df_15.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ad1e88c",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "print(classification_report(y_test, model.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0ae1e5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_3 = model.predict(df_14)\n",
    "pred_data['语音通话清晰度'] = y_pred_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b33eb101",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['语音通话稳定性', '是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '终端品牌', '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '前第3个月欠费金额', '通话过程中突然中断']\n",
    "df_16 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_16[i] = data_3[i]\n",
    "df_16 = pd.get_dummies(df_16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fdeb791",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = df_16.iloc[:, 1 :]\n",
    "y = df_16.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6783c909",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "clf1 = XGBClassifier(random_state=10)\n",
    "clf2 = RandomForestClassifier(random_state = 10)\n",
    "clf3 = LGBMClassifier(random_state = 10)\n",
    "eclf = VotingClassifier(estimators=[('xgb', clf1), ('rf', clf2), ('lgb', clf3)], voting = 'hard')\n",
    "y_test = y_test - 1\n",
    "for clf, label in zip([clf1, clf2, clf3, clf4, eclf], ['XGBBoosting', 'Random Forest', 'LightGBM', 'Ensemble']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring='accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a3b2da8",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf3], meta_classifier = clf2)\n",
    "for clf, label in zip([clf1, clf3, sclf], ['XGboost', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99b1c6f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf2, clf3], meta_classifier = clf1)\n",
    "for clf, label in zip([clf2, clf3, sclf], ['Random Forest', 'LightGBM', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ba901af",
   "metadata": {},
   "outputs": [],
   "source": [
    "sclf = StackingClassifier(classifiers = [clf1, clf2], meta_classifier = clf3)\n",
    "for clf, label in zip([clf1, clf2, sclf], ['XGboost', 'Random Forest', 'Stacking Model']):\n",
    "    scores = cross_val_score(clf, X_train, y_train - 1, cv = 5, scoring = 'accuracy')\n",
    "    print(\"Accuracy: %0.2f (+/- %0.2f) [%s]\" % (scores.mean(), scores.std(), label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5aed02b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10)\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85aec4f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "score = cross_val_score(model, X_train, y_train - 1, cv = 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96709ec9",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsRestClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs Rest Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ded4ab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf88d445",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams['font.sans-serif'] = ['SimHei']\n",
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "m1 = RandomForestClassifier(random_state = 10)\n",
    "m2 = DecisionTreeClassifier(random_state = 10)\n",
    "m3 = GradientBoostingClassifier(random_state = 10)\n",
    "m4 = LGBMClassifier(random_state = 10)\n",
    "m5 = XGBClassifier(random_state = 10)\n",
    "models =[m1, m2, m3, m4, m5]\n",
    "scores = []\n",
    "for model in models:\n",
    "    clf_multilabel = OneVsOneClassifier(model)\n",
    "    clf_multilabel.fit(X_train, y_train)\n",
    "    y_pred = clf_multilabel.predict(X_test)\n",
    "    score = accuracy_score(y_test, y_pred)\n",
    "    print('分类报告为:%s'%(model))\n",
    "    print(classification_report(y_test, y_pred))\n",
    "    print('%s五折交叉验证的分数为:'%(model))\n",
    "    print(cross_val_score(clf_multilabel, X_train, y_train, cv = 5))\n",
    "    scores.append(score)\n",
    "model_names = ('Random Forest',\n",
    "               'Decision Tree',\n",
    "               'GBDT',\n",
    "               'LightGBM',\n",
    "               'Xgboost')\n",
    "x_pos = np.arange(len(model_names))\n",
    "fig, ax = plt.subplots(figsize = (12, 8), dpi = 512)\n",
    "ax.grid(True)\n",
    "ax.set_title('One Vs One Classifier', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_xticks(x_pos)\n",
    "ax.set_xticklabels(model_names, font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylabel('model test scores', font = {'family' : 'STSong', 'size' : 14})\n",
    "ax.set_ylim([min(scores) * .9, max(scores) * 1.05])\n",
    "colors = ['b'] * (len(scores) - 1) + ['r']\n",
    "ax.bar(x_pos, scores, alpha = 0.5, color = colors)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1212fe8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsOneClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train - 1)\n",
    "y_pred = model.predict(X_test)\n",
    "print(cross_val_score(model, X_train, y_train - 1, cv = 5))\n",
    "print(classification_report(y_test - 1, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "778be797",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_17 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_17[i] = data[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e49228e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_17['是否遇到过网络问题'].replace(1, '是', inplace = True)\n",
    "df_17['是否遇到过网络问题'].replace(2, '否', inplace = True)\n",
    "df_17['通话中有杂音、听不清、断断续续'].replace(-1, '没有', inplace = True)\n",
    "df_17['通话中有杂音、听不清、断断续续'].replace(4, '有', inplace = True)\n",
    "df_17['有信号无法拨通'].replace(-1, '没有', inplace = True)\n",
    "df_17['有信号无法拨通'].replace(2, '有', inplace = True)\n",
    "df_17['办公室'].replace(-1, '不在', inplace = True)\n",
    "df_17['办公室'].replace(2, '在', inplace = True)\n",
    "df_17['通话过程中突然中断'].replace(-1, '没有', inplace = True)\n",
    "df_17['通话过程中突然中断'].replace(3, '有', inplace = True)\n",
    "df_17 = pd.get_dummies(df_17)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f13a8ce1",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols = ['语音通话稳定性' ,'是否遇到过网络问题', 'GPRS总流量（KB）', '当月ARPU', '前3月MOU', '当月MOU', '语音通话-时长（分钟）', 'mos质差次数', \\\n",
    "    '脱网次数', '通话中有杂音、听不清、断断续续', 'GPRS-国内漫游-流量（KB）', '未接通掉话次数', '有信号无法拨通', '客户星级标识', \\\n",
    "        '办公室', '通话过程中突然中断']\n",
    "df_18 = pd.DataFrame()\n",
    "for i in cols:\n",
    "    df_18[i] = data_3[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4637ccc",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_18 = pd.get_dummies(df_18)\n",
    "x = df_18.iloc[:, 1 :]\n",
    "y = df_18.iloc[:, 0 ]\n",
    "smote_model = SMOTE()\n",
    "x_smote, y_smote = smote_model.fit_resample(x, y)\n",
    "smote_df = pd.concat([y_smote, x_smote], axis=1)\n",
    "df = smote_df.copy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9af81ac7",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, 1 : ], df.iloc[:, 0], test_size = 0.3, shuffle = True, random_state = 10)\n",
    "model = OneVsRestClassifier(XGBClassifier(max_depth = 5, n_estimators = 300, random_state = 10))\n",
    "model.fit(X_train, y_train)\n",
    "print(classification_report(y_test, model.predict(X_test)))\n",
    "\n",
    "y_pred_4 = model.predict(df_17)\n",
    "pred_data['语音通话稳定性'] = y_pred_4\n",
    "\n",
    "pred_data.to_excel('./data/predict1.xlsx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
